package common.conf.Service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.orhanobut.logger.Logger;

/**
 * description: 定位服务
 * copyright: Hangzhou dingyi information technology co. LTD.
 * date: 2018/5/4
 */
public class LocationService extends Service {
    private boolean connecting = false;
    private CallBack callback = null;

    public interface CallBack {
        void onDataChange(AMapLocation aMapLocation);
        void onError(String errorInfo);
    }

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    //可在其中解析amapLocation获取相应内容。
//                    String mCity = aMapLocation.getCity();//城市信息
                    Logger.i("AMapLocationListener aMapLocation>>>" + aMapLocation.toString());

                    if (connecting == true) {
                        if (callback != null) {
                            callback.onDataChange(aMapLocation);
                            connecting = false;
                        }
                    }

                } else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                    String errorInfo = "AmapError location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo();
                    Logger.e(errorInfo);

                    if (callback != null) {
                        callback.onError(errorInfo);
                        connecting = true;
                    }
                }
            }
        }
    };
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("LocationService", "LocationService -> onCreate, Thread ID: " + Thread.currentThread().getId());

//        LocationManager locationManager =  (LocationManager) getSystemService(Context.LOCATION_SERVICE);;
//        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            Init();
//        } else {
//            Logger.w("未开启定位权限");
//        }

    }

    private void Init() {
        Log.i("LocationService", "LocationService -> Init, Thread ID: " + Thread.currentThread().getId());
        connecting = true;

        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Battery_Saving，低功耗模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        //获取一次定位结果：
        mLocationOption.setOnceLocation(true);//该方法默认为false。
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }


    @Override
    public IBinder onBind(Intent intent) {
        Log.i("LocationService", "LocationService -> onBind, Thread ID: " + Thread.currentThread().getId());
        return new Binder();
    }

    public class Binder extends android.os.Binder {
        public LocationService getService() {
            return LocationService.this;
        }
    }

    public void setCallBack(CallBack mCallBack) {
        this.callback = mCallBack;
    }

    @Override
    public void onDestroy() {
        Log.i("LocationService", "LocationService -> onDestroy, Thread ID: " + Thread.currentThread().getId());
        super.onDestroy();
        connecting = false;
    }


}